# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Lubomir 'Kuci' Kucera <kuci24-at-gmail-dot-com>

pkgname=gitlab-runner
pkgver=16.6.1
pkgrel=1
pkgdesc="The official GitLab CI runner written in Go"
arch=('loong64' 'x86_64')
url='https://gitlab.com/gitlab-org/gitlab-runner'
license=('GPL3')
depends=('ca-certificates' 'curl' 'git' 'glibc' 'tar')
makedepends=('git' 'go' 'git' 'mercurial' 'gox')
install=gitlab-runner.install
replaces=('gitlab-ci-multi-runner')
backup=('etc/gitlab-runner/config.toml')
noextract=("prebuilt-alpine-arm-${pkgver}.tar.xz"
           "prebuilt-alpine-arm64-${pkgver}.tar.xz"
           "prebuilt-alpine-s390x-${pkgver}.tar.xz"
           "prebuilt-alpine-x86_64-pwsh-${pkgver}.tar.xz"
           "prebuilt-alpine-x86_64-${pkgver}.tar.xz"
           "prebuilt-ubuntu-arm-${pkgver}.tar.xz"
           "prebuilt-ubuntu-arm64-${pkgver}.tar.xz"
           "prebuilt-ubuntu-s390x-${pkgver}.tar.xz"
           "prebuilt-ubuntu-x86_64-pwsh-${pkgver}.tar.xz"
           "prebuilt-ubuntu-x86_64-${pkgver}.tar.xz")
source=("git+https://gitlab.com/gitlab-org/gitlab-runner.git#tag=v${pkgver}"
        "prebuilt-alpine-arm-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-arm.tar.xz"
        "prebuilt-alpine-arm64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-arm64.tar.xz"
        "prebuilt-alpine-s390x-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-s390x.tar.xz"
        "prebuilt-alpine-x86_64-pwsh-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-x86_64-pwsh.tar.xz"
        "prebuilt-alpine-x86_64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-x86_64.tar.xz"
        "prebuilt-ubuntu-arm-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-arm.tar.xz"
        "prebuilt-ubuntu-arm64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-arm64.tar.xz"
        "prebuilt-ubuntu-s390x-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-s390x.tar.xz"
        "prebuilt-ubuntu-x86_64-pwsh-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-x86_64-pwsh.tar.xz"
        "prebuilt-ubuntu-x86_64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-x86_64.tar.xz"
        "gitlab-runner.service"
        "gitlab-runner.sysusers"
        "gitlab-runner.tmpfiles"
        "config.toml")
sha512sums=('SKIP'
            'cf3cd8f63a3563b5f9d138c74969e300e7df0c3ae1d3bd93a7ef37a710b5bc729cb798e3c4dca29daba1c243051d848c307a6cc42b18e46b778c5725798c87a0'
            'ceba1d1839abb9324bf5687d0f7993d0ce70c6ec1e1e88c7f3e4d4e2e9aa4721eeaab1a0bddf53691c94bb7416b44e167aa7c4bb71eb192e2c703e76ff98f100'
            '570996e417e4df8c9e199384304b5fe66fd10173e54d93aaa2aeedf3cb2034ab67abc423067b8673b7cb8ae3e74bca5f09841af72fd4bb10a66170b1d5017382'
            '9157b3d2279f5ae2bb5295adaea8d024a95823a16df9ad708b2bb8a8f195596465265e646b614a8306d5a5a4b59c3a9e1c57cee671255e544bf92159caad82c3'
            '0af7675759e5c6ac13c7f0b4fb028f230d1d2c7fd6ba6a009eefe21ef6689c3dddb82a85bff73fe4bcab3b9742459c86344b60bf28e89ccab729e69c4ef4762d'
            'ed1c739a23e5bffbca35fc65cde177302f9fd2395973b9eadad23e682cbd198fd715d335e8bdfd808aa4d8eb121f68edda18e65c7cdd5845f2ee19b36dce06a5'
            'e1bf69c3f4cb5378e6830715439b9978c0f6671874ef07c80080fc02324493adb539b80a5cb4fa2aea968428498d36f13b3fdebee4410055e422b37d7a97905f'
            'd62ff21a553a6e9e8354d41d6cea3029be9523bf5d1a029ac3c8a8e928dc0efc4d1d89eddc5d327a6c7a564a3ae02a9833848aeb9b65650373866ea34257cc12'
            'ecc003db0f2fb16504f3dd35399f654cc5cfd9d52e0842924a2daae304ba92a95e21de6fdc0f764532dabbb340ca70e5aed18e6ce71b400db9c32176ad3f9b51'
            '303fd008b1634fe489b87280a058a902598fdd80a92e130c5b33b27c5e18722bfe051eb16e0f179c90ec3ced2ea15125ad04b3bf46d0a956bca5f91aa22cbb57'
            'c0af374b9986895aedcfaee6c67cfad68f0f7289f87e4611358adaff59a2f349f55764fe28b2b1f61f8bfeb61126d4f90d433c626fdf9b826a2de6217f86574f'
            '8aa7f08702e99053c696fcc2aaba83beb9e9cd6f31973d82862db9350ac46df3a095377625d31fe909677525290d2de922d7a97930ed235774cb8f0da8944d40'
            '6751d9fa0b27172d1b419c4138f5ac15cbc7c9147653a7258cf1470216142c637210bb60608c7ed0974e0e4057e5ddeae32225df1bb36e7dd1f20fec71e33cc3'
            '9718b94bd0ddb09095ffb8c1e60ca1e9649dabb1747e7fc95e58e404b2f9effdeb4cfd759f5b904443dc53a4e18c02003c38f85584713deb49f6a6d1007503de')

prepare() {
  cd gitlab-runner

  local version=$(make version | grep "Current version:" | sed -n "s/.*: \(.*\)/\1/p")
  local revision=$(make version | grep "Current revision:" | sed -n "s/.*: \(.*\)/\1/p")
  local branch=$(make version | grep "Current branch:" | sed -n "s/.*: \(.*\)/\1/p")

  sed -i "s/var VERSION.*/var VERSION = \"$version\"/" common/version.go
  sed -i "s/var REVISION.*/var REVISION = \"$revision\"/" common/version.go
  sed -i "s/var BRANCH.*/var BRANCH = \"$branch\"/" common/version.go
}

build() {
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"

  cd gitlab-runner
  go mod edit -replace=github.com/cilium/ebpf=github.com/cilium/ebpf@v0.12.3
  go mod tidy
  go build -o gitlab-runner .
}

package() {
  cd gitlab-runner

  install -Dm644 "${srcdir}/config.toml" "${pkgdir}/etc/gitlab-runner/config.toml"
  install -Dm644 "${srcdir}/gitlab-runner.service" "${pkgdir}/usr/lib/systemd/system/gitlab-runner.service"
  install -Dm644 "${srcdir}/gitlab-runner.sysusers" "${pkgdir}/usr/lib/sysusers.d/gitlab-runner.conf"
  install -Dm644 "${srcdir}/gitlab-runner.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/gitlab-runner.conf"
  install -Dm755 gitlab-runner "${pkgdir}/usr/bin/gitlab-runner"
  install -Dm755 packaging/root/usr/share/gitlab-runner/clear-docker-cache "${pkgdir}/usr/share/gitlab-runner/clear-docker-cache"

  # Move prebuilt Docker images to hard-coded canonical location
  for image in prebuilt-{alpine,ubuntu}-{arm,arm64,s390x,x86_64-pwsh,x86_64}-${pkgver}.tar.xz; do
    install -Dm644 "${srcdir}/${image}" "${pkgdir}/usr/lib/gitlab-runner/helper-images/${image}"
  done
}
